package controllers

import dao._
import implicits.Implicits._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import shared.pojo.App.IdData
import shared.pojo.Gene.SymbolData
import tool.{FormTool, Tool}
import upickle.default.reader
import utils.Utils

import java.io.File
import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import upickle.default._

/** Created by yz on 10/12/2021
  */
class GeneController @Inject() (cc: ControllerComponents)(
    implicit val dataDao: DataDao,
    implicit val annoDao: AnnoDao,
    implicit val geneDao: GeneDao
) extends AbstractController(cc) {

  def getAllById = Action.async { implicit request =>
    val data = Tool.transformRq.transform(reader[IdData])
    dataDao
      .selectById(data.id)
      .flatMap { x =>
        geneDao.selectAll(x.gene)
      }
      .map { x =>
        val json = Utils.getArrayByTs(x)
        Ok(Json.toJson(json))
      }
  }

  def allBySymbol = Action.async { implicit request =>
    val data = Tool.transformRq.transform(reader[SymbolData])
    geneDao
      .selectAll(data.symbol)
      .map { x =>
        Ok(writeJs(x))
      }
  }

  def all = Action.async { implicit request =>
    geneDao
      .selectAll
      .map { x =>
        Ok(writeJs(x))
      }
  }

  def toIndex = Action { implicit request =>
    Ok(views.html.gene.index())
  }

  def getAll = Action.async { implicit request =>
    geneDao.selectAll.map { x =>
      val array = Utils.getArrayByTs(x)
      Ok(Json.toJson(array))
    }
  }

  def getAllGene = Action.async { implicit request =>
    geneDao.selectAllGene.map { x =>
      Ok(Json.toJson(x))
    }
  }

}
